package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.query.AnswerQueryObject;
import cn.wolfcode.trip.base.query.QuestionQueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 问题类资源
 */
@RestController
@RequestMapping("/questions")
public class QuestionController {

    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IAnswerService answerService;

    /**
     * 问题列表
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo list( QuestionQueryObject qo){
        //默认时间倒序
        qo.setOrderBy("releaseTime DESC");
        return questionService.query(qo);
    }

    @PostMapping
    public PageInfo listK(QuestionQueryObject qo){
        //默认时间倒序
        qo.setOrderBy("releaseTime DESC");
        return questionService.query(qo);
    }

    /**
     * 保存问题
     * @param question : 问题对象
     * @return
     */
    @PostMapping("/save")
    public JsonResult save(Question question){
        JsonResult jsonResult=new JsonResult();
        try {
            questionService.save(question);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 查询某个问题的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Question get(@PathVariable Long id){
        Question question = questionService.get(id);
        question.setPageViews(question.getPageViews()+1);
        questionService.updatePageViews(question);
        return question;
    }

    /**
     * 查询某个问题的回答数量
     * @param id
     * @return
     */
    @GetMapping("/{id}/answerNum")
    public int getAnswerNum(@PathVariable Long id){
        return answerService.getNumByQuestionId(id);
    }

    /**
     * 查询某个问题的回答
     * @return
     */
    @GetMapping("/{questionId}/answers")
    public PageInfo answerList( AnswerQueryObject qo){
        return answerService.query(qo);
    }

    /**
     * 保存回答
     * @param answer
     * @return
     */
    @PostMapping("/{questionId}/answer")
    public JsonResult save(Answer answer){
        JsonResult jsonResult=new JsonResult();
        try{
            answerService.save(answer);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 查询用户是否点了赞
     * @param userId 用户id
     * @param questionId 问题id
     * @return
     */
    @GetMapping("/likeRelation")
    public int likeRelation(Long questionId, Long userId){
       return questionService.getLikeRelation(userId,questionId);
    }

    /**
     * 保存点赞关系
     * @param userId : 用户id
     * @param questionId : 问题id
     * @return
     */
    @PostMapping("/saveLike")
    public JsonResult saveLike(Long userId,Long questionId){
        JsonResult jsonResult=new JsonResult();
        try{
            questionService.saveLikeRelation(userId,questionId);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 删除点赞关系
     * @param userId : 用户id
     * @param questionId : 问题id
     * @return
     */
    @DeleteMapping("/deleteLike")
    public JsonResult deleteLike(Long questionId,Long userId){
        JsonResult jsonResult=new JsonResult();
        try{
            questionService.deleteLikeRelation(userId,questionId);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 获取点赞数量
     * @param id 问题id
     * @return
     */
    @GetMapping("/{id}/getLikeNum")
    public int getLikeNum(@PathVariable Long id){
        return questionService.getLikeNum(id);
    }

}
